package com.dongdongsongcan.app.fragment;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.dongdongsongcan.app.DDApp;
import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.activity.LocationChooseActivity;
import com.dongdongsongcan.app.activity.ShopDetailActivity;
import com.dongdongsongcan.app.activity.SearchShopActivity;
import com.dongdongsongcan.app.activity.ShoplistActivity;
import com.dongdongsongcan.app.adapter.AutoPagerAdapter;
import com.dongdongsongcan.app.adapter.LabelAdapter;
import com.dongdongsongcan.app.adapter.ShoplistAdapter;
import com.dongdongsongcan.app.common.DDMessageDef;
import com.dongdongsongcan.app.control.ShopControl;
import com.dongdongsongcan.app.control.bean.CategoryData;
import com.dongdongsongcan.app.control.bean.LabelData;
import com.dongdongsongcan.app.control.bean.RecommendAdData;
import com.dongdongsongcan.app.control.bean.ShopData;
import com.dongdongsongcan.app.lib.pulltorefresh.PullToRefreshBase;
import com.dongdongsongcan.app.lib.pulltorefresh.PullToRefreshListView;
import com.su.dolphin.control.task.CtrlTask.IControlResponse;
import com.su.dolphin.core.notification.DpMessageCenter;
import com.su.dolphin.utils.ViewUtil;
import com.su.dolphin.utils.ui.AutoScrollViewPager;
import com.su.dolphin.utils.ui.HorizontalListView;
import com.su.dolphin.utils.ui.HorizontalListView.Orientation;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.utils.ui.indicator.CirclePageIndicator;
import com.su.dolphin.view.fragment.BaseFragment;

public class MainFragment extends BaseFragment implements DpMessageCenter.INotify {

    TextView mMain_titlebar;
    EditText mMain_searchbar;
    PullToRefreshListView mMain_shoplist;
    HorizontalListView mMain_labels;
    CirclePageIndicator mMain_indicator;
    AutoScrollViewPager mMain_pager;

    private AutoPagerAdapter mAutoPageAdapter;
    private ShoplistAdapter mShoplistAdapter;
    private LabelAdapter mLabelAdapter;
    private TextView mMain_shop_category;
    CategoryData mCategoryData;
    private TextView mMain_sort;
    private TextView mMain_tv_title_address;
    private int mOffset;
    private int PAGE_SIZE = 10;
    private String mOrderby;
    private String mShop_type;
    private double mSite_x, mSite_y;
    private View main_et_search;

    @Override
    protected int onSetContentView() {
        DpMessageCenter.i().register(this, DDMessageDef.N_LOCATION_CHANGE);
        return R.layout.fragment_main;
    }

    @Override
    public void initView() {
        main_et_search = getView().findViewById(R.id.main_et_search);
        mMain_tv_title_address = (TextView) getView().findViewById(
                R.id.main_tv_title_address);
        mMain_shoplist = (PullToRefreshListView) getView().findViewById(R.id.main_shoplist);
        View listHeader = ViewUtil.inflateView(
                R.layout.layout_main_shoplist_header, getActivity());
        mMain_shoplist.getRefreshableView().addHeaderView(listHeader);
        mMain_shoplist.setMode(PullToRefreshBase.Mode.BOTH);
        mMain_sort = (TextView) getView().findViewById(R.id.main_sort);
        mMain_shop_category = (TextView) getView().findViewById(
                R.id.main_shop_category);
        mMain_labels = (HorizontalListView) getView().findViewById(
                R.id.main_labels);
        mMain_labels.setOrientation(Orientation.HORIZONTAL);
        mMain_pager = (AutoScrollViewPager) getView().findViewById(
                R.id.main_pager);
        mMain_indicator = (CirclePageIndicator) getView().findViewById(
                R.id.main_indicator);
        mMain_pager.setAutoScrollDurationFactor(10);

        mAutoPageAdapter = new AutoPagerAdapter(getActivity());
        mMain_pager.setAdapter(mAutoPageAdapter);

        mShoplistAdapter = new ShoplistAdapter(getActivity());
        mMain_shoplist.setAdapter(mShoplistAdapter);

        mLabelAdapter = new LabelAdapter(getActivity());
        mMain_labels.setAdapter(mLabelAdapter);

    }


    @Override
    public void setListener() {
        main_et_search.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchShopActivity.startFrom(getActivity());
            }
        });
        mMain_shoplist.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                ;
                refreshShops();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getShops();
            }
        });
        mMain_tv_title_address.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                LocationChooseActivity.startFrom(getActivity());

            }
        });
        mMain_sort.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showSorts();
            }
        });
        mMain_shop_category.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mCategoryData != null) {
                    showCategoriesMenus();
                } else {
                    ShopControl.i().getCategoryDatas(
                            new IControlResponse<CategoryData>() {

                                @Override
                                public void onControlResponse(CategoryData data) {
                                    mCategoryData = data;
                                    showCategoriesMenus();
                                }
                            });
                }

            }

        });
        mMain_shoplist.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                ShopDetailActivity.startFrom(getActivity(), mShoplistAdapter
                        .getItem(position - 2));//注意header
            }
        });
        mMain_labels.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LabelData.Label label = mLabelAdapter.getItem(position);
                ShoplistActivity.startFrom(getActivity(), label);
            }
        });

    }

    @Override
    public void initData() {
//        if (DDApp.getDDLocation() != null) {
//            mMain_tv_title_address.setText("送至:" + DDApp.getDDLocation().getArea_name());
//            mSite_x = DDApp.getDDLocation().getSite_x();
//            mSite_y = DDApp.getDDLocation().getSite_y();
//        }
        ShopControl.i().getRecommendAds(
                new IControlResponse<RecommendAdData>() {

                    @Override
                    public void onControlResponse(RecommendAdData result) {
                        mAutoPageAdapter.setData(result.getResult());
                    }
                });
        ShopControl.i().getLabels((new IControlResponse<LabelData>() {

            @Override
            public void onControlResponse(LabelData result) {
                mLabelAdapter.setData(result.getResult());
            }
        }));
        //   refreshShops();
    }

    private void getShops() {
        ShopControl.i().getShops(null, mShop_type, mOrderby, mOffset, PAGE_SIZE, mSite_x, mSite_y,
                new IControlResponse<ShopData>() {

                    @Override
                    public void onControlResponse(ShopData data) {
                        mOffset = data.getOffset();
                        //       if (mOffset <= data.getNum()) {
                        mShoplistAdapter.addData(data.getResult());
                        //     } else {
                        //       TipsUtil.toast("已经是最后一页");
                        // }
                        mMain_shoplist.onRefreshComplete();

                    }
                });
    }

    @Override
    public void onClick(View arg0) {

    }

    @Override
    public void update(Bundle bundle) {

    }

    private void showSorts() {
        final String[] sorts = new String[]{"默认", "距离", "销量"};
        TipsUtil.showContextMenu(getActivity(), "选择排序", sorts,
                new Dialog.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mOrderby = sorts[which];
                        refreshShops();
                        mMain_sort.setText(mOrderby);

                    }
                });
    }

    private void showCategoriesMenus() {
        final String[] cats = mCategoryData.getResult()
                .toArray(new String[]{});
        TipsUtil.showContextMenu(getActivity(), "选择分类", cats,
                new Dialog.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TipsUtil.toast(menus[which]);

                        mShop_type = cats[which];
                        refreshShops();
                        mMain_shop_category.setText(mShop_type);
                    }
                });
    }

    private void refreshShops() {
        mOffset = 0;
        mShoplistAdapter.clear();
        getShops();
    }

    @Override
    public void onNotifyInner(DpMessageCenter.MCMessage mcmessage) {
        if (mcmessage.notificationID == DDMessageDef.N_LOCATION_CHANGE) {
            mMain_tv_title_address.setText("送至:" + DDApp.getDDLocation().getArea_name());
            mSite_x = DDApp.getDDLocation().getSite_x();
            mSite_y = DDApp.getDDLocation().getSite_y();
            refreshShops();
        }
    }
}
